<?php

namespace App\Logics;


use App\Models\User;
use App\Models\UserAgent;
use App\Service\Base\Logic;

class UserAgentLogic extends Logic
{

    // 认证操作：学校+机构


    // 列表查询
    public function getLists()
    {
        $event = $this->getAgentWhere();
        $where = $event[0];
        $bang  = $event[1];

        // 推荐
        $opt   = $this->option();
        $page  = $opt->getData('page', 0, 'intval');
        $rows  = $opt->getData('rows', 15, 'intval');
        $lists = $this->getAgent($where, $bang, $page, $rows);

        return $lists;
    }

    // 执行认证操作
    public function onAgent($myUid)
    {
        $option = $this->option();
        $_type  = $option->getData('agent_type', null, 'trim');
        $_allow = $option->getData('school_allow', null, 'trim');


        // 认证以前请先完善资料
        $userDb = new User();
        $info   = $userDb->where('id', $myUid)->first();
        $mobile = $info['mobile'];
        $option->throwIf(empty($mobile), '请先完善个人资料！');


        // 判断是否已经提交认证
        //$option->throwIf(empty($code), '请填写验证码！');
        $option->throwIf($info['verify'] == 1, '你已经通过认证审核！');
        $option->throwIf($info['is_agent'], '你已经申请认证,请等待审核！');
        $option->throwIf(empty($_type), '请选择认证类型！');
        $option->throwIf(($_type == 'school') && empty($_allow), '请学校请上传办学资质！');

        // 添加的数据
        $data = [
            'uid'          => $myUid,
            'agent_type'   => $_type,
            'agent_name'   => $option->getData("agent_name", null, "trim"),
            'city'         => $option->getData("city", null, "trim"),
            'grade'        => $option->getData("grade", null, "trim"),
            'telephone'    => $option->getData("telephone", null, "trim"),
            'address'      => $option->getData("address", null, "trim"),
            'description'  => $option->getData("description", null, "trim"),
            'license_img'  => $option->getData("license_img", null, "trim"),
            'school_allow' => $_allow,
            'is_pay'       => 0,
            'verify'       => 0,
            //'form_time'    => 0,
            //'end_time'     => 0,
            //'ctime'        => $opt->getData("ctime", null, "intval"),
            //'mtime'        => $opt->getData("mtime", null, "intval"),
        ];


        // 判断是否已经提交（可能重复提交） ->where('verify', 0)
        $agentDb   = new UserAgent();
        $agentInfo = $agentDb->where('uid', $myUid)->first();
        if($agentInfo){
            $option->throwIf(($agentInfo['verify'] == 1), '你已经申请认证！');
            //$option->throwIf(($agentInfo['verify'] == 0) && ($agentInfo['end_time'] > LARAVEL_TIME), '你已经申请认证,请等待审核！');
        }


        // 判断验证码
        //$smsDb = new SiteSms();
        //$where = ['mobile' => $mobile, 'code' => $code, "status" => "0"];
        //$sinfo = $smsDb->where($where)->orderBy("id", "DESC")->first();
        //$this->throwIf(empty($sinfo), '验证码错误！！');
        //$this->throwIf(LARAVEL_TIME - $sinfo->ctime > 600, '验证码超时！！');

        // 验证信息
        $rules  = ["agent_name" => 'required', "license_img" => 'required', "realname" => 'required', "position" => 'required', "areas" => 'required', "files" => 'required', "job_time" => 'required',];
        $rules  = ["agent_name" => 'required', "license_img" => 'required',];
        $msg    = ["agent_name" => '认证名称', "license_img" => '营业执照', "realname" => '真实姓名', "position" => '头衔', "areas" => '擅长领域', "description" => '描述', "files" => '证明文件', "job_time" => '从业年限',];
        $result = $userDb->validator($data, $rules, $msg);
        if($result === false){
            return $this->_returnFail($userDb->getError());
        }

        // 审核内容
        //$this->baiduTextCensor($data);

        // 保存
        $agentDb = new UserAgent();
        $res     = $agentDb->onAgent($myUid, $data);
        if($res === false){
            return $this->_returnFail($agentDb->getError());
        }

        return true;
    }

    ##############################################################################
    // 查询列表
    public function getAgent($where, $bang, $page = 0, $rows = 15)
    {
        // 模型
        $start = $page ? ($page - 1) * $rows : 0;
        $model = new UserAgent();

        // 执行
        /*$KCache = $model->cacheKey('lists', $where);
        $result = $model->cacheClass()->getCache($KCache);
        if(empty($result)){}*/
        if($page){
            $result = $model->bangField($bang)->listPage($where, $page, $rows, "id DESC");
        } else{
            $result = $model->bangField($bang)->getList($where, $rows, "id DESC", $start);
        }


        return $result;
    }

    // 列表查询条件
    protected function getAgentWhere()
    {
        $option  = $this->option();
        $where   = [];
        $bang    = [];
        $a_id    = $option->getData('agent_id', 0, 'intval');
        $c_id    = $option->getData('course_id', 0, 'intval');
        $uid     = $option->getData('uid', 0, 'intval');
        $_type   = $option->getData('agent_type', null, 'intval');
        $grade   = $option->getData("grade", null, "trim");
        $_verify = $option->getData('verify', null, 'intval');
        $name    = $option->getData('agent_name', null, 'trim');
        $is_prop = $option->getData('is_propose', null, 'intval');  // 推荐
        $keyword = $option->getData('keyword', null, 'trim');
        if($a_id){
            $bang[]  = 'agent_id';
            $where[] = ['agent_id', '=', $a_id];
        }
        if($c_id){
            $bang[]  = 'course_id';
            $where[] = ['course_id', '=', $c_id];
        }
        if($uid){
            $bang[]  = 'uid';
            $where[] = ['uid', '=', $uid];
        }
        if(!is_null($_verify)){
            $bang[]  = 'verify';
            $where[] = ['verify', '=', $_verify];
        } else{
            $bang[]  = 'verify';
            $where[] = ['verify', '=', 1];
        }
        if(!is_null($_type)){
            $bang[]  = 'agent_type';
            $where[] = ['agent_type', '=', $_type];
        }
        if($grade){
            $bang    = 'grade';
            $where[] = ['grade', '=', $grade];
        }
        if($name){
            $bang    = false;
            $where[] = ['agent_name', 'like', "%".$name."%"];
        }
        if(!is_null($is_prop)){
            $bang[]  = 'is_propose';
            $where[] = ['is_propose', '=', $is_prop];
        }

        return [$where, $bang];
    }

}


